import { useResponseSuccess } from '~/utils/response';

export default eventHandler((event) => {
  const query = getQuery(event);
  const page = Number.parseInt(query.page as string) || 1;
  const pageSize = Number.parseInt(query.pageSize as string) || 10;
  const contextId = query.contextId as string;
  const serviceId = query.serviceId as string;
  const status = query.status as string;
  const customerName = query.customerName as string;

  // 模拟管理端预约数据
  const mockBookings = [
    {
      id: 'booking_1',
      bookingNo: 'BK202401001',
      serviceId: '1',
      serviceName: '温泉泡浴',
      contextId: '1',
      contextName: '温泉洗浴',
      customerName: '张三',
      phoneNumber: '13800138001',
      wechatId: 'zhangsan123',
      memberCard: 'VIP001',
      bookingDate: '2024-01-15',
      timeSlot: '14:00',
      status: 'CONFIRMED',
      queueTicket: 'A001',
      queuePosition: 3,
      estimatedWaitTime: '30分钟',
      remarks: '希望安静的环境',
      createTime: '2024-01-10 10:00:00',
      updateTime: '2024-01-10 10:00:00',
    },
    {
      id: 'booking_2',
      bookingNo: 'BK202401002',
      serviceId: '4',
      serviceName: '标准间',
      contextId: '2',
      contextName: '酒店公寓',
      customerName: '李四',
      phoneNumber: '13800138002',
      wechatId: 'lisi456',
      memberCard: 'VIP002',
      bookingDate: '2024-01-16',
      timeSlot: '15:00',
      status: 'PENDING',
      remarks: '需要无烟房间',
      createTime: '2024-01-11 14:30:00',
      updateTime: '2024-01-11 14:30:00',
    },
    {
      id: 'booking_3',
      bookingNo: 'BK202401003',
      serviceId: '8',
      serviceName: '宠物体检',
      contextId: '4',
      contextName: '宠物医疗',
      customerName: '王五',
      phoneNumber: '13800138003',
      wechatId: 'wangwu789',
      memberCard: 'VIP003',
      bookingDate: '2024-01-17',
      timeSlot: '09:30',
      status: 'COMPLETED',
      remarks: '猫咪体检',
      createTime: '2024-01-12 09:00:00',
      updateTime: '2024-01-12 09:00:00',
    },
    {
      id: 'booking_4',
      bookingNo: 'BK202401004',
      serviceId: '6',
      serviceName: '中餐包间',
      contextId: '3',
      contextName: '餐厅服务',
      customerName: '赵六',
      phoneNumber: '13800138004',
      wechatId: 'zhaoliu101',
      memberCard: 'VIP004',
      bookingDate: '2024-01-18',
      timeSlot: '18:00',
      status: 'CANCELLED',
      remarks: '8人包间',
      createTime: '2024-01-13 16:00:00',
      updateTime: '2024-01-13 16:00:00',
    },
    {
      id: 'booking_5',
      bookingNo: 'BK202401005',
      serviceId: '2',
      serviceName: '全身按摩',
      contextId: '1',
      contextName: '温泉洗浴',
      customerName: '钱七',
      phoneNumber: '13800138005',
      wechatId: 'qianqi202',
      memberCard: 'VIP005',
      bookingDate: '2024-01-19',
      timeSlot: '16:00',
      status: 'CONFIRMED',
      queueTicket: 'A002',
      queuePosition: 1,
      estimatedWaitTime: '5分钟',
      remarks: '需要女技师',
      createTime: '2024-01-14 11:00:00',
      updateTime: '2024-01-14 11:00:00',
    },
  ];

  // 根据条件过滤
  let filteredBookings = mockBookings;

  if (contextId) {
    filteredBookings = filteredBookings.filter(
      (booking) => booking.contextId === contextId,
    );
  }

  if (serviceId) {
    filteredBookings = filteredBookings.filter(
      (booking) => booking.serviceId === serviceId,
    );
  }

  if (status) {
    filteredBookings = filteredBookings.filter(
      (booking) => booking.status === status,
    );
  }

  if (customerName) {
    filteredBookings = filteredBookings.filter((booking) =>
      booking.customerName.includes(customerName),
    );
  }

  // 分页
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  const paginatedBookings = filteredBookings.slice(start, end);

  return useResponseSuccess({
    list: paginatedBookings,
    total: filteredBookings.length,
  });
});
